package com.example.xd.commen.utils;

import android.app.Activity;
import android.os.Process;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther XuDan on 2019/4/11.
 */
public class ActivityUtil {
    private List<Activity> activityList = new ArrayList<>();
    private static ActivityUtil instance;


    private ActivityUtil() {
    }

    public static synchronized ActivityUtil getInstance() {
        if (null == instance) {
            instance = new ActivityUtil();
        }
        return instance;
    }

    /**
     * 添加activity
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (activityList == null)
            activityList = new ArrayList<>();
        activityList.add(activity);
    }

    /**
     * 移除activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (activityList != null)
            activityList.remove(activity);
    }

    /**
     * 遍历所有，退出
     */
    public void exitSystem() {
        for (Activity activity : activityList) {
            if (activity != null) {
                activity.finish();
            }
            android.os.Process.killProcess(Process.myPid());
            System.exit(0);
        }

    }
}
